#include "yui.h"

#include "ylcd.h"

#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
// #include <threads.h>

// #define MAX_PTHARR 50
// static thrd_t ptharr[MAX_PTHARR]={0};

static void * yclock_thrd_fn(void *);
static void getTime(char timestr[], int size);

yclock* create_clock(int x, int y, int width, int height, font *fp, int32_t pixels, uint32_t background_color, uint32_t fontcolor)
{
    if (fp == NULL){
        fp = fontLoad("../font/simfang.ttf");
        if (fp == NULL) return NULL;
    }

    // 创建clock 结构体
    yclock* pclo = (yclock*)malloc(sizeof(yclock));

    // 创建 bitmap
    pclo->bmap = createBitmapWithInit(width, height, 3, background_color<<8);
    pclo->fp = fp;
    pclo->pixels = pixels;
    pclo->x = x;
    pclo->y = y;
    pclo->clo = fontcolor;
    pclo->bclo = background_color;
    pclo->isRun = true;

    // 线程函数
    pclo->pfun = (void*)yclock_thrd_fn;
    pclo->pth = -1;

    return pclo;
}

// 时钟开始运行
void startClock(yclock *clo)
{
    int ret = pthread_create(&clo->pth, NULL, clo->pfun, clo);
    if (ret != 0){

        return ;
    }
}

// 时钟停止运行
void stopClock(yclock *clo)
{
    // thrd_exit(0);
    // clo->pth = -1;
    clo->isRun = false;
}

void destroy_yclock(yclock *clo)
{
    fontUnload(clo->fp);
    destroyBitmap(clo->bmap);
    free(clo);
}

GraphRect create_Btn(int x, int y, int width, int height, font *fp, int32_t pixels, uint32_t background_color, uint32_t fontcolor, char *text)
{
    if (fp == NULL){
        fp = fontLoad("../font/simfang.ttf");
        if (fp == NULL){
            printf("fp == NULL\n");
            return (GraphRect){};
        }
    }

    // 打印按钮
    fontSetSize(fp, pixels);
    bitmap *screen = createBitmapWithInit(width-2, pixels, 3, background_color<<8);
    fontPrint(fp, screen, 0, 0, text, fontcolor<<8, 0);
    // 绘制按钮边框
    GraphRect ret = draw_rect(x, y, width, height, 0x0);
    // 绘制按钮背景色
    clear_rect(background_color, x+1, y+1, width-2, height-2);
    // 绘制按钮内容
    y = y + (height-pixels)/2;
    draw_graph(screen->map, x+1, y+1, width-2, pixels, screen->byteperpixel, false);
    
    
    // 释放 bitmap
    destroyBitmap(screen);
    
    return ret;
}

static void * yclock_thrd_fn(void *pv)
{
    char timestr[50]={1, '\0'};
    yclock *clo = (yclock*)pv;
    uint32_t mapsize = clo->bmap->width*clo->bmap->height*clo->bmap->byteperpixel;
    uint8_t *backmap = (uint8_t*)calloc(1, mapsize);
    memcpy(backmap, clo->bmap->map, mapsize);

    while (clo->isRun) {
        // 需要重新刷新  以免字体重叠
        memcpy(clo->bmap->map, backmap, mapsize);

        // 获取时间
        getTime(timestr, 50);
        if (timestr[0] == '\0'){
            stopClock(clo);
        }

        // 写入时间
        fontSetSize(clo->fp, clo->pixels);
        fontPrint(clo->fp, clo->bmap, 0, 0, timestr, clo->clo, 0);

        // 打印时间
        draw_graph(clo->bmap->map, clo->x, clo->y, clo->bmap->width, clo->bmap->height, clo->bmap->byteperpixel, false);
    }
    // thrd_exit(0);
    clo->pth = -1;
    free(backmap);
    return NULL;
}

static void getTime(char timestr[], int size)
{
    time_t currentTime = time(NULL);
    if (currentTime == -1){
        timestr[0] = '\0';
        return ;
    }
    struct tm* localTime = localtime(&currentTime);
    if (localTime == NULL) {
        timestr[0] = '\0';
        return ;
    }
    // 转换字符串
    strftime(timestr, size, "%H:%M:%S", localTime);
}